package com.leet.code.lcof;

import com.algorithm.model.TreeNode;
import com.algorithm.model.TreeNodeService;

public class Code54 {
    public static void main(String[] args) {
        TreeNode treeNode = TreeNodeService.getTreeByArray(new Integer[]{3, 1, 4, null, 2});
        System.out.println(new Code54().search(treeNode));

    }

    public int kthLargest(TreeNode root, int k) {
        this.k = k;
        return search(root);
    }

    int k;
    int i = 0;

    public Integer search(TreeNode node) {
        Integer j = null;
        if (node.left != null) {
            j = search(node.left);
        }
        if (j != null) {
            return j;
        }
        i++;
        if (i == k) {
            return node.val;
        }

        if (node.right != null) {
            return search(node.right);
        }

        return null;
    }


}
